/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.carbswang.ohos.numberpickerview;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.text.Font;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.agp.window.service.WindowManager;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;

import java.util.Calendar;
import java.util.GregorianCalendar;

import cn.carbswang.ohos.numberpickerview.library.NumberPickerView;

/**
 * TimePickerAbility
 *
 * @since 2021-08-11
 */
public class TimePickerAbility extends Ability
    implements Component.ClickedListener, NumberPickerView.OnValueChangeListener {
    private NumberPickerView mPickerViewH;
    private NumberPickerView mPickerViewM;
    private NumberPickerView mPickerViewD;
    private Button mButtonInfo;
    private Button mButtonInfo2;
    private Button mButton4;
    private MyEventHandler mHandler;
    private ToastDialog toastDialog;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        WindowManager.getInstance().getTopWindow().get()
            .setStatusBarColor(getColor(ResourceTable.Color_colorPrimaryDark));
        getWindow().setStatusBarVisibility(Component.HIDE);
        setUIContent(ResourceTable.Layout_ability_time_picker);
        mHandler = new MyEventHandler(EventRunner.current());

        mPickerViewH = (NumberPickerView) findComponentById(ResourceTable.Id_picker_hour);
        mPickerViewM = (NumberPickerView) findComponentById(ResourceTable.Id_picker_minute);
        mPickerViewD = (NumberPickerView) findComponentById(ResourceTable.Id_picker_half_day);
        mPickerViewH.setOnValueChangedListener(this);
        mPickerViewM.setOnValueChangedListener(this);
        mPickerViewD.setOnValueChangedListener(this);

        Font font = FontUtil.parseAttributes(getContext());
        mPickerViewH.setContentTextTypeface(font);
        mPickerViewM.setContentTextTypeface(font);
        mPickerViewD.setContentTextTypeface(font);
        mPickerViewH.setHintTextTypeface(font);
        mPickerViewM.setHintTextTypeface(font);
        mPickerViewD.setHintTextTypeface(font);

        mButtonInfo = (Button) findComponentById(ResourceTable.Id_button_get_info);
        mButtonInfo2 = (Button) findComponentById(ResourceTable.Id_show_info_button);
        mButton4 = (Button) findComponentById(ResourceTable.Id_button4);
        mButtonInfo.setClickedListener(this);
        mButton4.setClickedListener(this);
        initTime();
    }

    private void initTime() {
        GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar.getInstance();
        int h = calendar.get(Calendar.HOUR_OF_DAY);
        int m = calendar.get(Calendar.MINUTE);
        int d = h < 12 ? 0 : 1;
        h = h % 12;

        setData(mPickerViewH, 0, 11, h);
        setData(mPickerViewM, 0, 59, m);
        setData(mPickerViewD, 0, 1, d);
    }

    private void setData(NumberPickerView picker, int minValue, int maxValue, int value) {
        picker.setMinValue(minValue);
        picker.setMaxValue(maxValue);
        picker.setValue(value);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_button_get_info:
                String h = mPickerViewH.getContentByCurrValue();
                String m = mPickerViewM.getContentByCurrValue();
                String d = mPickerViewD.getContentByCurrValue();
                toast(h + getString(ResourceTable.String_hour_hint) + " "
                    + m + getString(ResourceTable.String_minute_hint) + " " + d);
                break;
            case ResourceTable.Id_button4:
                mPickerViewH.setHintTextTypeface(Font.DEFAULT_BOLD);
                mPickerViewH.invalidate();
                break;
            default:
                break;
        }
    }

    @Override
    public void onValueChange(NumberPickerView picker, int oldVal, int newVal) {
        if (EventRunner.current() == EventRunner.getMainEventRunner()) {
            mButtonInfo2.setText(getString(ResourceTable.String_current_thread_name) + Thread.currentThread().getName()
                + System.lineSeparator() + getString(ResourceTable.String_current_picked_value) + newVal);
        } else {
            String message = getString(ResourceTable.String_current_thread_name) + Thread.currentThread().getName()
                + System.lineSeparator() + getString(ResourceTable.String_current_picked_value) + newVal;
            mHandler.sendEvent(InnerEvent.get(0, message));
        }
    }

    /**
     * MyEventHandler
     *
     * @since 2021-07-29
     */
    private class MyEventHandler extends EventHandler {
        MyEventHandler(EventRunner runner) throws IllegalArgumentException {
            super(runner);
        }

        @Override
        protected void processEvent(InnerEvent event) {
            super.processEvent(event);
            if (event == null) {
                return;
            }
            switch (event.eventId) {
                case 0:
                    mButtonInfo2.setText((String) event.object);
                    break;
                default:
                    break;
            }
        }
    }

    private void toast(String value) {
        if (toastDialog != null) {
            toastDialog.remove();
            toastDialog.destroy();
            toastDialog = null;
        }
        DirectionalLayout toastLayout = (DirectionalLayout) LayoutScatter.getInstance(getContext())
            .parse(ResourceTable.Layout_picker_toast_layout, null, false);
        Text tvValue = (Text) toastLayout.findComponentById(ResourceTable.Id_tv_value);
        Text tvContent = (Text) toastLayout.findComponentById(ResourceTable.Id_tv_content);
        tvValue.setText(value);
        tvValue.setText(value);
        tvContent.setVisibility(Component.HIDE);

        toastDialog = new ToastDialog(getContext());
        toastDialog.setContentCustomComponent(toastLayout);
        toastDialog.setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        toastDialog.setAlignment(LayoutAlignment.BOTTOM);
        toastDialog.setTransparent(true);
        toastDialog.show();
    }
}